//
//  LGStatuesCacheTool.m
//  微博
//
//  Created by MS on 15-12-29.
//  Copyright (c) 2015年 MS. All rights reserved.
//

#import "LGStatuesCacheTool.h"
#import "FMDB.h"
#import "LGAccount.h"
#import "LGAccountTool.h"
#import "LGStatuesParam.h"
#import "LGStatus.h"
#import "MJExtension.h"
static FMDatabase * _db;
@implementation LGStatuesCacheTool

+(void)initialize
{
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *filePath = [path stringByAppendingPathComponent:@"stutas.sqlite"];
    
    _db = [FMDatabase databaseWithPath:filePath];
    [_db open];
//    if ([_db open]) {
//        NSLog(@"打开成功");
//    }
//    else{
//        NSLog(@"打开失败");
//
//    }
    
}


+(void)saveDataWithArray:(NSArray *)statues
{
//   BOOL flag =
    [_db executeUpdate:@"create table if not exists t_statues (id integer primary key autoincrement,idstr text,access_token text,dict blob);"];
    
//    if (flag) {
//        NSLog(@"创建成功");
//    }
//    else{
//         NSLog(@"创建失败");
//    }
    
    for (NSDictionary * dic in statues) {
        NSString * idstr = dic[@"idstr"];
        NSString * access_token =[LGAccountTool account].access_token;
        NSData * dict =[NSKeyedArchiver archivedDataWithRootObject:dic];
        
       [_db executeUpdate:@"insert into t_statues (idstr,access_token,dict) values(?,?,?)",idstr,access_token,dict];
        
//        if (flag) {
//            NSLog(@"插入成功");
//        }
//        else{
//            NSLog(@"插入失败");
//        }
    }
    
//    
//    NSUInteger count = [_db intForQuery:@"select count(*) from t_statues"];
    
   // NSLog(@"记录数--------%ld",count);
}

+(NSArray *)statuesWithParam:(LGStatuesParam *)parama
{
    
    NSMutableArray * statuesArray = [NSMutableArray array];
    NSString * sql =[NSString stringWithFormat:@"select * from t_statues where access_token='%@' order by idstr desc limit 20;",parama.access_token];
    if (parama.since_id) {
        sql =[NSString stringWithFormat:@"select * from t_statues where access_token='%@' and idstr >'%@' order by idstr desc limit 20;",parama.access_token,parama.since_id];
    }else if(parama.max_id)
    {
        sql =[NSString stringWithFormat:@"select * from t_statues where access_token='%@' and idstr <='%@' order by idstr desc limit 20;",parama.access_token,parama.max_id];
    }

    FMResultSet * set = [_db executeQuery:sql];
    while ([set next]) {
        
        NSData * data = [set dataForColumn:@"dict"];
        NSDictionary * dic = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        LGStatus * statues = [LGStatus objectWithKeyValues:dic];
        [statuesArray addObject:statues];
    }
    
    return statuesArray ;
}

+(void) deleteOldData
{
    NSUInteger count = [_db intForQuery:@"select count(*) from t_statues"];
    
    //保留最新的20条，其他删除
    NSString * sql =[NSString stringWithFormat:@"delete from t_statues where id in (select id from t_statues order by idstr desc limit 20,%ld-20);",count];

//   BOOL flag =
    [_db executeUpdate:sql];
//    if (flag) {
//        NSLog(@"删除成功");
//    }
//    else{
//        NSLog(@"删除失败");
//    }

}
@end
